package com.vken.blog.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.vken.blog.model.Blog;
import com.vken.blog.model.Entry;
import com.vken.blog.service.BlogManager;
import com.vken.blog.service.EntryManager;
import com.vken.blog.util.Const;
import com.vken.blog.util.StringUtil;

public class SubscribeAction extends ActionSupport {
	private static final long serialVersionUID = -6228651208717236814L;
	private BlogManager blogManager;
	private EntryManager entryManager;
	private InputStream xmlStream;

	public void setBlogManager(BlogManager blogManager) {
		this.blogManager = blogManager;
	}

	public void setEntryManager(EntryManager entryManager) {
		this.entryManager = entryManager;
	}

	public InputStream getXmlStream() {
		return xmlStream;
	}

	public void setXmlStream(InputStream xmlStream) {
		this.xmlStream = xmlStream;
	}

	public String execute() {
		Blog blog = blogManager.get(Const.BLOG_ID);
		if (blog.getStatus() == 0) {
			return "closed";
		}

		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", new Locale("en"));
		HttpServletRequest request = ServletActionContext.getRequest();

		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		xml.append("<rss version=\"2.0\">\n");
		xml.append("<channel>\n");
		xml.append("<title>");
		xml.append(blog.getName());
		xml.append("</title>\n");
		xml.append("<link>");
		xml.append(getUrl(request));
		xml.append("</link>\n");
		xml.append("<description>");
		xml.append(blog.getDescription());
		xml.append("</description>\n");
		xml.append("<generator>");
		xml.append("vken Blog System");
		xml.append("</generator>\n");

		List<Entry> entrys = entryManager.getListByBlogId(Const.BLOG_ID, 1, blog.getEntryNumber(),
				1).getList();
		for (Entry entry : entrys) {
			xml.append("<item>\n");
			xml.append("<title>");
			xml.append(StringUtil.escapeXml(entry.getTitle()));
			xml.append("</title>\n");
			xml.append("<link>");
			xml.append(getUrl(request));
			xml.append("/load?id=");
			xml.append(entry.getId());
			xml.append("</link>\n");
			xml.append("<description>\n");
			xml.append(StringUtil.escapeXml(entry.getContent()));
			xml.append("</description>\n");
			xml.append("<pubDate>");
			xml.append(sdf.format(entry.getCreatedTime()));
			xml.append("</pubDate>\n");
			xml.append("</item>\n");
		}
		xml.append("</channel>\n");
		xml.append("</rss>");

		try {
			xmlStream = new ByteArrayInputStream(xml.toString().getBytes("utf8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "xml";
	}

	private String getUrl(HttpServletRequest request) {
		String contextPath = (request.getContextPath().equals("/")) ? "" : request.getContextPath();
		StringBuffer url = new StringBuffer();
		url.append(request.getScheme() + "://");
		url.append(request.getServerName());
		url.append(":");
		url.append(request.getServerPort());
		url.append(contextPath);
		return url.toString();
	}
}